/*
 * threads.c
 *
 *  Created on: 10.01.2013
 *      Author: Michael
 */

#include "threads.h"
#include <msp430.h>

// FIXME audio booster pack
#define MASK45                  (BIT3+BIT5+BIT6+BIT7)
#define MASK80                  BIT4
#define MASK100                 (BIT3+BIT4+BIT6+BIT7)
#define MASK135                 BIT5
#define MASK225                 (BIT3+BIT4+BIT5+BIT7)
#define MASK260                 BIT6
#define MASK280                 (BIT3+BIT4+BIT5+BIT6)
#define MASK315                 BIT7
const unsigned char startSequence[8] = { MASK315, MASK280, MASK260, MASK225,
		MASK135, MASK100, MASK80, MASK45 };

void runThread1(void) {
	WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
	P1DIR |= BIT0;
	while (1) {
		P1OUT = BIT0;
	}
}

void runThread2(void) {
	WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
	P1DIR |= BIT6;
	while (1) {
		P1OUT = BIT6;
	}
}

void runThread3(void) {
	// FIXME not working yet
	WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
	P1DIR |= BIT3+BIT4+BIT5+BIT6+BIT7+BIT0;
	P1OUT = 0; // Turn off all LEDs initially
	unsigned char i = 8;

	/* Fast counter-clockwise sequence */
	while (i) {
		i--;
		P1OUT = startSequence[i];
		TACCR0 = TAR + 500; // 50ms
	}
	P1OUT = 0; // Turn off all LEDs
}

static Thread_t createThread(uint16_t id) {
	Thread_t pThread;
	pThread.id = id;
	pThread.state = THREAD_READY;
	return pThread;
}

Thread_t createThread1(uint16_t id) {
	Thread_t pThread = createThread(id);
	pThread.run = runThread1;
	return pThread;
}

Thread_t createThread2(uint16_t id) {
	Thread_t pThread = createThread(id);
	pThread.run = runThread2;
	return pThread;
}

Thread_t createThread3(uint16_t id) {
	Thread_t pThread = createThread(id);
	pThread.run = runThread3;
	return pThread;
}
